﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanceTool.Components;
using FinanceTool.Entities;
using FinanceTool.Components.Mappers;
using FinanceTool.WinAppUI.Utilities;

namespace FinanceTool.WinAppUI
{
    public partial class Form1 : Form
    {
        private List<Transaction> _transactions;

        public Form1()
        {
            InitializeComponent();
            btnRefresh.Enabled = false;
            openFileDialog1.FileOk += new CancelEventHandler(openFileDialog1_FileOk);
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            txtSourceFile.Text = openFileDialog1.FileName;
            btnRefresh.Enabled = true;
        }

        private void btnOpenSourceFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.Height = 1047;
            this.Width = 916;
            
            bool @continue = MethodHelper.TryExecute(
                () =>
                {
                    GetData();
                }, 
                () =>
                {
                    MessageBox.Show("Fout tijdens uitlezen bronbestand. Controleer of het bronbestand klopt.", "Fout", MessageBoxButtons.OK);
                });

            if (!@continue) return; // In case of an error, stop execute.

            MethodHelper.TryExecute(
                () =>
                {
                    BindData(false, false); // Bind debet data
                    BindData(false, true); // Bind credit data         
                    BindData(true); // Bind credit vs debet data
                },
                () =>
                {
                    MessageBox.Show("Fout tijdens het presenteren van de gegevens.", "Fout", MessageBoxButtons.OK);
                });
        }

        private void GetData()
        {
            if (string.IsNullOrEmpty(txtSourceFile.Text))
            {
                MessageBox.Show("Please choose a source file!");
                return;
            }

            var reader = SourceFileReaderFactory.Create(SourceFileType.Ing, txtSourceFile.Text.Trim());
            _transactions = reader.ReadTransactions();
        }

        private void BindData(bool bindCreditVsDebet, bool isCredit = false)
        {
            if (_transactions == null) return;

            var mapper = new TransactionToXyMapper(_transactions, isCredit);
            // Map values
            mapper.MapToXyValues();

            if (bindCreditVsDebet)
            {
                creditDebetChart.Series[0].Points.DataBindXY(mapper.DebetCreditValuesX, mapper.DebetCreditValuesY);
                lblTotalCredit.Text = mapper.TotalCredit.ToString("C");
                lblTotalDebet.Text = mapper.TotalDebet.ToString("C");
            }
            else
            {
                // Populate series data
                decimal[] yValues = mapper.ValuesY;
                string[] xValues = mapper.ValuesX;

                if (isCredit)
                {
                    creditPieChart.Series[0].Points.DataBindXY(xValues, yValues);
                    creditGraphChart.Series[0].Points.DataBindXY(xValues, yValues);
                }
                else
                {
                    debetPieChart.Series[0].Points.DataBindXY(xValues, yValues);
                    debetGraphChart.Series[0].Points.DataBindXY(xValues, yValues);
                }
            }
        }
    }
}
